//
//  SAPVideoCollectionViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/2.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPVideoCollectionViewCell.h"
#import "Theme-PrefixHeader.pch"
@implementation SAPVideoCollectionViewCell
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self tableViews];
        [self addSubview:self.tableView];
        [self header];
        [self.tableView headerBeginRefreshing];
        
    }return self;
}

- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [_tableView registerClass:[SAPVideoTableViewCell class] forCellReuseIdentifier:@"SAPVideoTableViewCellIndentifier"];
    [_tableView release];
    [self footer];
}

- (void)header {
            _HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
            _HUD.color = [UIColor blackColor];
            _HUD.dimBackground = YES;
            _HUD.labelText = @"正在加载";
    [_tableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SAPNetWorkTool getWithUrl:video parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                self.viewOneModel = [[SAPVideoOneModel alloc] initWithDataSource:dic];
                _HUD.hidden = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            
            [_tableView headerEndRefreshing];
            
        });
    }];
}
- (void)footer {
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
            NSString *string = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.5/news/videolist-pm1-vt0-s30-lastid%@.json", self.viewTwoModel.lastid];
        
            [SAPNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                if (result) {
                    
                
                NSDictionary *dic = [result valueForKey:@"result"];
                SAPVideoOneModel *oneModel = [[SAPVideoOneModel alloc] initWithDataSource:dic];
                    NSMutableArray *arr = [[NSMutableArray alloc] init];
                    for (NSDictionary *dic in oneModel.listArray) {
                        [arr addObject:dic];
                    }NSArray *array = [NSArray arrayWithArray:arr];
                [self.viewOneModel.listArray addObjectsFromArray:array];
                    
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView footerEndRefreshing];
                     [self.tableView reloadData];

                });
             }
        } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
        });
        
    }];
    
    
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 69);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewOneModel.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAPVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPVideoTableViewCellIndentifier"];
    SAPVideoTwoModel *videoTwoModel = self.viewOneModel.listArray[indexPath.row];
    self.viewTwoModel = self.viewOneModel.listArray[indexPath.row];
    cell.videoTwoModel = videoTwoModel;
        return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    SAPVideoTwoModel *twoModel = self.viewOneModel.listArray[indexPath.row];
    
    NSString *str = [NSString stringWithFormat:@"http://v.autohome.com.cn/v_4_%@.html", twoModel.id];
    
    
    [self.delegate passVideoValue:str];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
@end
